
import { globalUtils } from '../common/js/global-utils/global-utils-all';

/* SOURCE-CODE-START */

"use strict";

CurrentRunningApp.addSharing('menu', function (ns) {

  ns.convertToTreeData = convertToTreeData;

  /**
   * @description 转换成树形数据
   * @param {Array|Object} menuData 
   * @param {function} [callbackFn]
   * @returns {Array|Object}
   */
  function convertToTreeData(menuData, callbackFn) {
    if (globalUtils.isNullOrUndefined(menuData)) {
      throw new Error('argument#0 "menuData" is null/undefined');
    }

    var treeData;

    if (menuData instanceof Array) {
      treeData = createTreeNodeData(menuData, callbackFn);
    } else {
      treeData = createTreeNodeData([menuData], callbackFn);
    }

    return treeData;
  }

  /**
   * @description 转换成树形数据
   * @param {Array|Object} menuData 
   * @param {function} [callbackFn]
   * @returns {Array|Object}
   */
  function createTreeNodeData(menuData, callbackFn) {
    if (globalUtils.isNullOrUndefined(menuData)) {
      throw new Error('argument#0 "menuData" is null/undefined');
    }

    if (menuData instanceof Array) {
      var treeData = [];

      menuData.forEach(function (value, index) {
        // 递归转换成树形数据
        var nodeData = createTreeNodeData(value, callbackFn);

        var callbackResult;
        if (!globalUtils.isNullOrUndefined(callbackFn)) {
          var nodeInfoData = nodeData.nodeInfo.data;
          callbackResult = callbackFn(nodeData, 'menu', nodeInfoData);
        }

        if (!(callbackResult === false)) {
          treeData.push(nodeData);
        }
      });

      return treeData;
    }

    var childFunctions = menuData.childFunctions;
    // 创建菜单节点的数据
    var menuNodeData = createMenuNodeData(menuData, callbackFn);

    if (!globalUtils.isNullOrUndefined(childFunctions) && childFunctions.length > 0) {
      menuNodeData.state.opened = true;
      menuNodeData.children = globalUtils.emptyArrayIfNullOrUndefined(menuNodeData.children);

      childFunctions.forEach(function (functionData) {
        // 创建功能节点的数据
        var functionNode = createFunctionNodeData(functionData, callbackFn);

        var callbackResult;
        if (!globalUtils.isNullOrUndefined(callbackFn)) {
          callbackResult = callbackFn(functionNode, 'function', functionData)
        }

        if (!(callbackResult === false)) {
          menuNodeData.children.push(functionNode);
        }
      });
    }

    return menuNodeData;
  }

  /**
   * 创建菜单节点的数据
   * @param {*} menuData 
   * @param {function} callbackFn
   * @returns 
   */
  function createMenuNodeData(menuData, callbackFn) {
    if (globalUtils.isNullOrUndefined(menuData)) {
      throw new Error('argument#0 "menuData" is null/undefined');
    }

    var menuName = menuData.menuName;
    var childMenus = menuData.childMenus;
    var nodeId = 'menu_' + menuData.id;

    var nodeData = {
      id: nodeId,
      text: menuName,
      icon: 'bi bi-menu-button-wide',
      state: {
        opened: false
      },
      nodeInfo: {
        type: 'menu',
        data: menuData,
        childMenus: childMenus
      }
    };

    if (!globalUtils.isNullOrUndefined(childMenus) && childMenus.length > 0) {
      nodeData.state.opened = true;
      nodeData.children = createTreeNodeData(childMenus, callbackFn);
    }

    return nodeData;
  }

  /**
   * 创建功能节点的数据
   * @param {*} functionData 
   * @returns 
   */
  function createFunctionNodeData(functionData) {
    if (globalUtils.isNullOrUndefined(functionData)) {
      throw new Error('argument#0 "functionData" is null/undefined');
    }

    var functionName = functionData.functionName;
    //var nodeId = 'function_' + functionData.id;

    var nodeData = {
      //id: nodeId,
      text: functionName,
      icon: 'bi bi-gear',
      state: {
        opened: false
      },
      nodeInfo: {
        type: 'function',
        data: functionData
      }
    };

    return nodeData;
  }

});


/* SOURCE-CODE-END */
